﻿
using OnlineMealOrdering.Domain.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OnlineMealOrdering.Common;

namespace OnlineMealOrdering.Domain
{
    public class StoreCommentEntity : AuditableEntity
    {
        public virtual UserEntity User { get; set; }

        public virtual StoreOnlineEntity Store { get; set; }

        public string Content { get; set; }
        
        public EnumCommentType Type { get; set; }

        //引用的回复
        public virtual StoreCommentEntity Relevance { get; set; }

        //是否显示
        public bool Enable { get; set; }

        public static StoreCommentEntity Create(string userCode, int storeid, string content, EnumCommentType type, int? relevanceId)
        {
            var user = DomainContext.Current.EntitySet<UserEntity>().SingleOrDefault(a => a.UserCode == userCode);
            if (user == null)
                BusinessException.Create("找不到用户Code:{0}", userCode);

            var store = DomainContext.Current.EntitySet<StoreOnlineEntity>().SingleOrDefault(a => a.Id == storeid);
            if (user == null)
                BusinessException.Create("找不到店铺ID:{0}", storeid);

            var rel = DomainContext.Current.EntitySet<StoreCommentEntity>().SingleOrDefault(a => a.Id == relevanceId);
            if (rel == null && relevanceId.HasValue)
                BusinessException.Create("找不到引用回复ID:{0}", relevanceId);

            var entity = new StoreCommentEntity();
            entity.Content = content;
            entity.Enable = true;
            if (relevanceId.HasValue)
                entity.Relevance = rel;
            entity.Store = store;
            entity.Type = EnumCommentType.StoreOnline;
            entity.User = user;
            entity.Enable = true;

            return entity;
        }

        public void CloseComment()
        {
            Enable = false;
        }

    }
}
